/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.config.javabeans;

import opiam.admin.faare.exception.ConfigurationException;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


/**
 * Bean corresponding to the triggers_config.xml file.
 */
public class JBTriggers implements java.io.Serializable
{
    /** Global post triggers. */
    private Map postTriggersMap = new HashMap();

    /** Global pre triggers. */
    private Map preTriggersMap = new HashMap();

    /** Triggers per profile. */
    private Map triggerProfilesMap = new HashMap();

    /** Configuration directory. */
    private String directory;

    /**
     * Adds a pre trigger to list.
     *
     * @param jbTrigger trigger to add
     */
    public void addPreTrigger(JBTrigger jbTrigger)
    {
        // initialiser les plugins
        jbTrigger.setPost(false);
        jbTrigger.getPlugin().initialize(jbTrigger.getParam(), directory);

        preTriggersMap.put(jbTrigger.getName().trim().toLowerCase(), jbTrigger);
    }

    /**
     * Adds a post trigger to list.
     *
     * @param jbTrigger trigger to add
     */
    public void addPostTrigger(JBTrigger jbTrigger)
    {
        // initialiser les plugins
        jbTrigger.setPost(true);
        jbTrigger.getPlugin().initialize(jbTrigger.getParam(), directory);

        postTriggersMap.put(jbTrigger.getName().trim().toLowerCase(), jbTrigger);
    }

    /**
     * Adds a list of triggers for a profile.
     *
     * @param prof list of triggers for a profile.
     */
    public void addTriggerProfile(JBTriggerProfile prof)
    {
        triggerProfilesMap.put(prof.getName().trim().toLowerCase(), prof);
    }

    /**
     * Returns the post triggers table.
     * @return post triggers table.
     */
    public Map getPostTriggersMap()
    {
        return postTriggersMap;
    }

    /**
     * Returns the pre triggers table.
     * @return pre triggers table.
     */
    public Map getPreTriggersMap()
    {
        return preTriggersMap;
    }

    /**
     * Displays triggers configuration.
     *
     * @return String formatted triggers configuration
     */
    @Override
	public String toString()
    {
        StringBuffer buf = new StringBuffer();
        java.util.Iterator it = null;

        if (postTriggersMap != null)
        {
            buf.append("Map components of postTriggersMap = ");
            buf.append(System.getProperty("line.separator"));
            it = postTriggersMap.keySet().iterator();

            Object key;

            while (it.hasNext())
            {
                buf.append("...");
                key = it.next();
                buf.append(key);
                buf.append(" = ");
                buf.append(postTriggersMap.get(key));
                buf.append(System.getProperty("line.separator"));
            }
        }

        if (preTriggersMap != null)
        {
            buf.append("Map components of preTriggersMap = ");
            buf.append(System.getProperty("line.separator"));
            it = preTriggersMap.keySet().iterator();

            Object key;

            while (it.hasNext())
            {
                buf.append("...");
                key = it.next();
                buf.append(key);
                buf.append(" = ");
                buf.append(preTriggersMap.get(key));
                buf.append(System.getProperty("line.separator"));
            }
        }

        if (triggerProfilesMap != null)
        {
            buf.append("Map components of triggerProfilesMap = ");
            buf.append(System.getProperty("line.separator"));
            it = triggerProfilesMap.keySet().iterator();

            Object key;

            while (it.hasNext())
            {
                buf.append("...");
                key = it.next();
                buf.append(key);
                buf.append(" = ");
                buf.append(triggerProfilesMap.get(key));
                buf.append(System.getProperty("line.separator"));
            }
        }

        return buf.toString();
    }

    /**
     * Computes the list of triggers for each profile.
     */
    public void compute()
    {
        Iterator itProf = triggerProfilesMap.values().iterator();
        JBTriggerProfile prof;
        JBTrigger trigger;
        String triggerName;
        String key;

        while (itProf.hasNext())
        {
            // boucler sur chaque profile
            prof = (JBTriggerProfile) itProf.next();

            Iterator itTrig = prof.getTriggers().iterator();

            while (itTrig.hasNext())
            {
                // boucler sur chaque nom de trigger dans le profile
                triggerName = (String) itTrig.next();
                key = triggerName.trim().toLowerCase();
                trigger = (JBTrigger) preTriggersMap.get(key);

                if (trigger != null)
                {
                    prof.addPreTrigger(trigger);
                }
                else
                {
                    trigger = (JBTrigger) postTriggersMap.get(key);

                    if (trigger == null)
                    {
                        throw new ConfigurationException("triggerName " +
                            triggerName + " not defined in trigger config");
                    }
                    else
                    {
                        prof.addPostTrigger(trigger);
                    }
                }
            }
        }
    }

    /**
     * Gets the list of pre triggers for a profile and an operation type.
     *
     * @param profileName profile name
     * @param opName operation type
     *
     * @return list of triggers, or null
     */
    public List findPreTriggersByProfile(String profileName, String opName)
    {
        String key = profileName.trim().toLowerCase();
        JBTriggerProfile prof = (JBTriggerProfile) triggerProfilesMap.get(key);

        if (prof == null)
        {
            return null;
        }
        else
        {
            return prof.findPreTriggersByOpName(opName);
        }
    }

    /**
     * Gets the list of post triggers for a profile and an operation type.
     *
     * @param profileName profile name
     * @param opName operation type
     *
     * @return list of triggers, or null
     */
    public List findPostTriggersByProfile(String profileName, String opName)
    {
        String key = profileName.trim().toLowerCase();
        JBTriggerProfile prof = (JBTriggerProfile) triggerProfilesMap.get(key);

        if (prof == null)
        {
            return null;
        }
        else
        {
            return prof.findPostTriggersByOpName(opName);
        }
    }

    /**
     * Returns the configuration directory.
     * @return directory
     */
    public String getDirectory()
    {
        return directory;
    }

    /**
     * Sets the configuration directory.
     * @param adirectory The directory to set
     */
    public void setDirectory(String adirectory)
    {
        this.directory = adirectory;
    }
}
